package com.itzol.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzol.config.Result;
import com.itzol.entity.Users;
import com.itzol.mapper.ConsumeMapper;
import com.itzol.service.ConsumeService;
import com.itzol.service.UsersService;
import com.itzol.util.Jwtutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author linxin
 * @since 2021-12-14
 */
@RestController
public class UsersController {
    @Autowired
    private UsersService usersService;
//    登录接口
    @PostMapping("/login")
    public Result login(@RequestBody Users users, HttpServletResponse response){
        System.out.println("输出:"+users);
        Result result = usersService.login(users);
        if (result.getCode()==200){
            String jwt = Jwtutil.createToken(users.getUserName());
            response.setHeader("Authorization", jwt);
            response.setHeader("Access-control-Expose-Headers", "Authorization");
        }
       return result;
    }
    //检验token 有效性
    @GetMapping("/checkToken")
    public Boolean checkToken(HttpServletRequest request){
        String str = request.getHeader("token");
        return Jwtutil.checkToken(str);
    }
    //分页查询 列表数据
    @PostMapping("/getuser/{page}/{size}")
    public Result GetUser(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<Users> pagesize = new Page<>(page-1,size);
        Page<Users> result = usersService.page(pagesize);
        return Result.succ(result.getRecords());
    }
    //新增用户数据
    @PostMapping("/adduser")
    public Result AddUser(@RequestBody Users users){
        Boolean b=usersService.save(users);
        if(b){
            return Result.succ("新增成功");
        }
        else {
          return Result.erreo("新增失败");
        }
    }
    //编辑页 根据id 查询信息并返回
    @PostMapping("/editor/{id}")
    public  Result editor(@PathVariable("id") Integer id){
        return Result.succ(usersService.getById(id));
    }
    //根据前端传递的id 更新该id的信息
    @PostMapping("/editoruser/{id}")
     public Result editoruser(@RequestBody Users users,@PathVariable("id")Integer id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        boolean result=usersService.saveOrUpdate(users,wrapper);
        if(result){
            return Result.succ(users.getName()+"信息修改成功");
        }
        else {
            return Result.erreo("修改失败,请重新修改");
        }

    }
    //获取所有用户的电话号码
    @GetMapping("/getphone")
    public Result GetPhone(){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.select("Phone");
        List<Users> list= usersService.list(wrapper);
        return Result.succ(list);
    }
    @Autowired
    private ConsumeMapper consumeMapper;
    //根据id 查询用户的消费记录
    @PostMapping("/getconsume/{id}")
    public Result GetConsume(@PathVariable("id")Integer id){
        return Result.succ(consumeMapper.GetConsumeVo(id));
    }


}

